package com.xunyoutest.shop.ui.activity;

import android.content.Intent;
import android.graphics.PixelFormat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.xunyoutest.shop.R;
import com.xunyoutest.shop.base.BaseActivity;
import com.xunyoutest.shop.ui.presenter.MainPresenter;
import com.xunyoutest.shop.ui.view.MainView;
import com.xunyoutest.shop.widget.StatueLayout;
import com.tencent.smtt.sdk.WebView;

import butterknife.BindView;

/**
 * Created by admin on 2018/9/20.
 */

public class MainActivity extends BaseActivity<MainPresenter> implements MainView {
    @BindView(R.id.statueLayout)
    StatueLayout statueLayout;
    WebView webView;

    private long firstTime = 0;

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected StatueLayout setStatueLayout() {
        return statueLayout;
    }

    @Override
    protected MainPresenter createPresenter() {
        return new MainPresenter(this,this,this);
    }

    @Override
    protected void initView() {
        //x5WebView视频播放优化（这个对宿主没什么影响，建议声明）
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        webView = new WebView(this);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        webView.setLayoutParams(params);
        webView.setHorizontalScrollBarEnabled(false);//水平不显示
        webView.setVerticalScrollBarEnabled(false); //垂直不显示
        try {
            webView.getX5WebViewExtension().setScrollBarFadingEnabled(false);//隐藏滚轮
        }catch (Exception e){
            e.printStackTrace();
        }
        statueLayout.addView(webView, 0);
        basePresenter.requestPermissions();
        basePresenter.initView();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        basePresenter.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();
            return false;
        } else if (keyCode == KeyEvent.KEYCODE_BACK && !webView.canGoBack()) {
            long secondTime = System.currentTimeMillis();
            if (secondTime - firstTime < 2000) {
                finish();
            } else {
                showToast("再按一次退出程序");
                firstTime = System.currentTimeMillis();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        basePresenter.onRestart();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        basePresenter.onDestroy();
        if (webView != null) {
            webView.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            webView.clearHistory();
            webView.destroy();
            webView = null;
        }
    }

    @Override
    public WebView webView() {
        return webView;
    }
}
